package io;

import java.io.*;
import java.util.Scanner;

public class IODemo14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件:");
        String srcPath = scanner.nextLine();
        System.out.println("请输入要复制的目标文件:");
        String destPath = scanner.nextLine();
        //路径合法性判断
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        if(!srcFile.isFile()) {
            System.out.println("输入的源文件有误!");
            return;
        }
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("输入的目标路径有误!");
            return;
        }

        //复制操作
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                outputStream.write(buffer,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("文件复制成功!地址: " + destFile.getAbsolutePath());
    }
}
